<template>
  <!-- 修改/新增 组件 -->
  <el-form ref="form" :model="form" label-width="80px" :rules="rules">
    <el-form-item label="文章标题" prop="title">
      <el-input v-model="form.title" placeholder="请输入文章标题" />
    </el-form-item>
    <el-form-item label="文章内容" prop="articleBody">
      <quillEditor
        v-model="form.articleBody"
        :options="editorOption"
        @change="onEditorChange($event)"
      />
    </el-form-item>
    <el-form-item label="视频地址">
      <el-input v-model="form.videoURL" placeholder="请输入视频地址" />
    </el-form-item>
    <el-form-item style="text-align: right;margin-bottom:0px;margin-top:60px">
      <el-button @click="cancelFn">取消</el-button>
      <el-button type="primary" @click="onSubmit">确认</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor' // 引入富文本
import { add, detail, update } from '@/api/hmmm/articles'
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  ['blockquote'], // 引用  代码块-----['blockquote', 'code-block']
  ['code-block', 'image', 'link'] // 链接、图片、视频
]
export default {
  name: 'Articles',
  components: {
    quillEditor
  },
  props: {
    articleid: {
      type: [Number, String],
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: { // 修改/新增/数据
        title: '',
        articleBody: '',
        videoURL: ''
      },
      editorOption: {
        //  富文本编辑器配置
        modules: {
          toolbar: toolbarOptions
        },
        theme: 'snow',
        placeholder: '请输入正文'
      },
      rules: { // 表单验证规则
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (this.newText.length === 0) {
                callback(new Error('请输入文章内容'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      },
      newText: '' // 富文本内容
    }
  },
  created() {
    this.detailArticle() // 数据回填
  },
  methods: {
    // 点击确定添加/修改技巧
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        if (!this.isEdit) {
          this.addArticle()
        } else {
          this.editArticle()
        }
      })
    },

    // 添加技巧
    async addArticle() {
      const res = await add(this.form)
      // console.log(res)
      this.$message.success(res.data.id ? '添加成功' : '添加失败')
      this.$emit('close')
    },

    // 修改技巧数据回填
    async detailArticle() {
      if (this.isEdit) {
        const res = await detail({ id: this.articleid })
        this.form = res.data
      }
    },

    // 修改技巧
    async editArticle() {
      const res = await update(this.form)
      this.$message.success(res.data.success === true ? '修改成功' : '修改失败')
      this.$emit('close')
    },

    // 取消按钮
    cancelFn() {
      this.$emit('closeCancel')
    },

    // 富文本验证
    onEditorChange({ quill, html, text }) {
      this.newText = text
      this.$refs.form.validateField('articleBody')
    },

    // 清空数据和验证规则
    clearDataFn() {
      this.form = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped lang='less'>
.quill-editor{
  height: 180px;
  margin-bottom: 50px;
}
</style>
